package com.zz.HospitalRegisterSystem;

import org.omg.PortableInterceptor.LOCATION_FORWARD;

import javax.print.Doc;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;

public class HospitalManager {
    //1.系统需要记录全部的预约信息
    private ArrayList<Department> allDepartments = new ArrayList <>();
    //2.医院需要记录全部的预约详情
    private ArrayList<Appointment> appointments = new ArrayList <>();
    private Scanner sc = new Scanner(System.in);

    public void start(){
        while(true){
            System.out.println("===========欢迎进入仁爱医院信息管理系统===========");
            System.out.println("1.科室管理-添加科室");
            System.out.println("2.科室管理-删除科室");
            System.out.println("3.科室管理-修改科室");
            System.out.println("4.医生管理-录入医生");
            System.out.println("5.医生管理-医生坐诊设置（可设置当天和未来6天的坐诊情况)");
            System.out.println("6.医生管理-展示全部医生的坐诊详情（当前和未来6天的坐诊详情)");
            System.out.println("7.医生管理-挂号预约");
            System.out.println("8.搜索某个医生当前和未来6天内的病人预约详情(展示每天预约病人的具体信息)");
            System.out.println("退出该系统");
            System.out.println("请输入操作命令");
            switch (sc.next()){
                case "1":
                    //增加科室
                    addDepartment();
                    break;
                case "2":
                    //删除科室
                    break;
                case "3":
                    //修改科室
                    break;
                case "4":
                    addDoctor();
                    //录入医生
                    break;
                case "5":
                    //医生坐诊设置
                    break;
                case "6":
                    //展示所有医生的坐诊信息
                    break;
                case "7":
                    //挂号预约
                    break;
                case "8":
                    //搜索某个医生未来七天的工作安排
                    break;
                case "9":
                    //退出系统
                    return;
                default:
                    //输入非法命令
                    System.out.println("不存在该操作，请重新输入操作命令:");
                    break;
            }
        }
    }

    //增加科室
    private void addDepartment(){
        System.out.println("==========增加的科室==========");
        OUT:
        while(true){
            System.out.println("请输入新的科室名：");
            String name = sc.next();
            for(int i = 0; i < allDepartments.size(); i++){
                Department department = allDepartments.get(i);
                if(department.getName().equals(name)){
                    System.out.println("当前科室已经存在！");
                    continue OUT;
                }
            }
            Department department = new Department();
            department.setName(name);
            allDepartments.add(department);
            System.out.println("添加新的科室成功！");
            break;
        }
    }

    private Department getDepartMenuByUser(){
        if(allDepartments.size() == 0){
            System.out.println("当前无科室可选择");
            return null;
        }
        while(true){
            System.out.println("1.请选择科室：");
            for(int i = 0; i < allDepartments.size(); i++){
                Department department = allDepartments.get(i);
                System.out.println((i+1) + "," + department.getName());
            }
            System.out.println("请输入科室");
            //2.接收命令
            int command = sc.nextInt();
            if(command < 1 || command > allDepartments.size()){
                System.out.println("输入有误，请重新输入");
                continue;
            }
            Department department = allDepartments.get(command - 1);
            return department;
        }
    }
    //添加医生
    private void addDoctor(){
        System.out.println("=========新增医生========");
        while (true) {
            Doctor doctor = new Doctor();
            //先选择科室;
            Department department = getDepartMenuByUser();
            if(department == null)
                return;
            //得到科室
            doctor.setDepartmentName(department.getName());

            //录入医生编号
            doctor.setDoctorId(UUID.randomUUID().toString());

            System.out.println("请输入医生的姓名：");
            String name = sc.next();
            doctor.setName(name);

            System.out.println("请输入医生的性别：");
            String gender = sc.next();
            doctor.setGender(gender);

            System.out.println("请输入医生的年龄");
            int age = sc.nextInt();
            doctor.setAge(age);

            System.out.println("请输入医生的特长:");
            String specialty = sc.next();
            doctor.setSpecialty(specialty);

            System.out.println("请输入医生的入职日期（格式：yyyy-MM-dd):");
            String joinDateString = sc.next();
            LocalDate joinDate = LocalDate.parse(joinDateString);
            doctor.setJoinDate(joinDate);

            department.getDoctors().add(doctor);
            System.out.println("恭喜您添加" + name + "医生成功！");
            break;

        }
    }

    //设置医生坐诊时间
    private void setDoctorJob(){
        System.out.println("=======设置医生的坐诊时间=======");
        Department department = getDepartMenuByUser();

        //选择医生
        ArrayList<Doctor> doctors = new ArrayList <>();
        if(doctors.size() == 0){
            System.out.println("当前科室无医生");
            return;
        }

        while(true){
            System.out.println("当前科室医生如下：");
            for(int i =0; i < doctors.size(); i++){
                Doctor doctor = doctors.get(i);
                System.out.println((i + 1) + "," + doctor.getName());
            }
            System.out.println("请输入：");
            //2接收命令
            int command = sc.nextInt();
            if(command < 1 || command > doctors.size()){
                System.out.println("选择有误，请重新确认");
                continue;
            }
            Doctor doctor = doctors.get(command - 1);

            //为这个医生设置坐诊情况
            ArrayList<Schedule> schedules = new ArrayList <>();

            //更新未来七天时间
            updateSchedule(schedules);

            //修改坐诊信息，依次展示这个医生的坐诊详情
            for(int i = 0; i < schedules.size(); i++){
                Schedule schedule = schedules.get(i);
                updateDoctorSchedules(schedule);
            }
            break;
        }
    }

    private void updateDoctorSchedules(Schedule schedule) {
        LocalDate today = schedule.getToday();
        System.out.println(today + "的安排如下:");
        if(!schedule.isUpdate()){
            System.out.println("未排班\t\t\t");
        }else{
            System.out.println("\t上午\t");
            if(schedule.isMorning()){
                System.out.println("坐诊时间：" + schedule.getMstart()
                + "-" + schedule.getMend() + "总数/预约数：" + schedule.getmTotalNumber() );
            }
        }
    }

    private void updateSchedule(ArrayList<Schedule> schedules) {
        if(schedules.size() == 0){
            for(int i = 0; i <= 6; i++){
                Schedule schedule = new Schedule();
                LocalDate  now = LocalDate.now();
                schedule.setToday(now.plusDays(i));
                schedules.add(schedule);
            }
            return;
        }

        //去除过期时间
        for(int i = 0; i < schedules.size(); i++){
            Schedule schedule = schedules.get(i);
            LocalDate now = LocalDate.now();
            LocalDate current = schedule.getToday();
            if(current.equals(now)){
                break;
            }
            if(current.isBefore(now)){
                schedules.remove(schedule);
                i--;
            }
        }

        //补全当前和未来6天的时间
        LocalDate last = schedules.get(schedules.size() - 1).getToday();
        for(int i =0; i < 7 - schedules.size(); i++){
            Schedule schedule = new Schedule();
            schedule.setToday(last.plusDays(i + 1));
            schedules.add(schedule);
        }
    }
}
